fix(analytics): add posthog ingest proxy and document env vars#3915
fix(analytics): add posthog ingest proxy and document env vars#3915waleedlatif1 wants to merge 1 commit intostagingfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
PR SummaryLow Risk Overview Documents the optional analytics env vars in Written by Cursor Bugbot for commit 14dd40c. Configure here. |
|
Closing — the /ingest proxy is already handled at the infrastructure level since session recordings are flowing correctly. The next.config.ts rewrite is only needed when Next.js itself needs to proxy PostHog traffic. Keeping the .env.example docs only. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
| { | ||
| source: '/ingest/:path*', | ||
| destination: 'https://us.i.posthog.com/:path*', | ||
| }, |
There was a problem hiding this comment.
New rewrite rules are dead code due to existing route
Low Severity
The new /ingest rewrite rules will never be reached because an existing App Router route handler at app/ingest/[[...path]]/route.ts already handles all /ingest/* requests. Next.js evaluates filesystem routes (including API route handlers) before flat-array rewrites, so the optional catch-all route always takes precedence. These rewrites are effectively dead code and duplicate the proxy logic already implemented in the route handler.
Greptile SummaryThis PR attempts to fix client-side PostHog event 404s by adding
Confidence Score: 4/5Safe to merge as-is (behavior is unchanged since the route handler wins), but the rewrites should be removed to prevent future confusion and regression risk. The apps/sim/next.config.ts — the two new Important Files Changed
Sequence DiagramsequenceDiagram
participant Browser
participant NextJS as Next.js Router
participant RH as app/ingest/[[...path]]/route.ts
participant Rewrite as next.config.ts rewrites
participant PH as PostHog (us.i.posthog.com)
Browser->>NextJS: POST /ingest/decide
Note over NextJS: Checks filesystem routes first (afterFiles)
NextJS->>RH: Route Handler matches /ingest/[[...path]]
Note over RH: Strips cookie & connection headers<br/>Sets Host header<br/>Streams body with duplex:'half'
RH->>PH: Proxied request (no cookies)
PH-->>RH: Response
RH-->>Browser: Forwarded response
Note over Rewrite: These rewrites never fire —<br/>shadowed by the route handler above
Reviews (1): Last reviewed commit: "fix(analytics): add posthog ingest proxy..." | Re-trigger Greptile |
| source: '/ingest/static/:path*', | ||
| destination: 'https://us-assets.i.posthog.com/static/:path*', | ||
| }, | ||
| { | ||
| source: '/ingest/:path*', | ||
| destination: 'https://us.i.posthog.com/:path*', | ||
| }, |
There was a problem hiding this comment.
Rewrites conflict with (and are shadowed by) the existing route handler
PR #3187 (c471627ce) explicitly replaced these exact rewrites with app/ingest/[[...path]]/route.ts specifically for "reliable body streaming." That route handler already proxies all /ingest/* requests to us.i.posthog.com / us-assets.i.posthog.com, and does so with careful header handling (strips cookie, connection headers; sets Host; removes content-encoding on the response).
When rewrites() returns a plain array, Next.js treats them as afterFiles rewrites — meaning they only apply when no matching filesystem route exists. Since app/ingest/[[...path]]/route.ts is a valid App Router Route Handler for exactly these paths, Next.js will route traffic through the handler first, and these two rewrites will never execute.
In short, the new rewrites are dead code. If they were ever made to execute (e.g., if the route handler is later removed or if a future Next.js version changes precedence), they would silently regress the intentional improvements from #3187: request bodies may not stream reliably, and auth/session cookies would be forwarded to PostHog rather than stripped.
Consider removing these two rewrite entries to avoid confusion and keep the proxy logic consolidated in the route handler.


Summary
/ingestreverse proxy rewrites tonext.config.ts— without these, all client-side PostHog events were 404ing since the client is configured to send to/ingestNEXT_PUBLIC_POSTHOG_KEYandNEXT_PUBLIC_POSTHOG_ENABLEDin.env.exampleType of Change
Testing
Tested manually
Checklist